package com.yylending.las.personal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.YJKD;
import com.yylending.las.authority.gestures.GesturesActivity;
import com.yylending.las.authority.gestures.GesturesPresenter;
import com.yylending.las.authority.password.ModifyPwdActivity;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.main.MainActivity;
import com.yylending.las.personal.logout.LogoutPresenter;

public class SecurityActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, LogoutPresenter.View{
	private final static String TAG = "SecurityActivity";
	private final static int LOADER_ID = 1;

	@BindView(R.id.security_logout)
	Button mLogout;
	@BindView(R.id.security_fingerprint_cb)
	CheckBox mSecurityFingerprintCb;
	@BindView(R.id.security_fingerprint)
	RelativeLayout mSecurityFingerprint;

	private LogoutPresenter mPresenter;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, SecurityActivity.class);
		aty.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_security);
		ButterKnife.bind(this);
		setTitle(R.string.title_activity_security);
		if (YJKD.login()) {
			mLogout.setVisibility(View.VISIBLE);
		} else {
			mLogout.setVisibility(View.GONE);
		}
		if(YJKD.getInstance().isFingerprintAvailable()){
			mSecurityFingerprint.setVisibility(View.VISIBLE);
			//mSecurityFingerprintCb.setOnCheckedChangeListener(this);
		}else{
			mSecurityFingerprint.setVisibility(View.GONE);
		}
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mSecurityFingerprintCb.setChecked(YJKD.getInstance().isFingerprintOpen() && YJKD.getInstance().hasFingerprint());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID:
				mPresenter = (LogoutPresenter) data;
				mPresenter.attach(this);
				break;
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID:
				return new PresenterLoader<>(this, new LogoutPresenter.Generator());
		}
		return null;
	}

	@OnClick({R.id.security_modify_password, R.id.security_gestures_password, R.id.security_logout, R.id.security_fingerprint_cb})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.security_modify_password:
				ModifyPwdActivity.start(this);
				break;
			case R.id.security_gestures_password:
				StatService.onEvent(this, StatIDSet.EVENT_ID_GESTURES_PASSWORD);
				GesturesActivity.start(this);
				break;
			case R.id.security_logout:
				showTwoBtnDialog(getString(R.string.confirm_logout), getString(R.string.confirm), getString(R.string.cancel), mLogoutDialog, true);
				break;
			case R.id.security_fingerprint_cb:
				judgeCheckbox(mSecurityFingerprintCb.isChecked());
				break;
		}
	}

	private void judgeCheckbox(boolean checked){
		if(checked){
			if(GesturesPresenter.readStatus() && YJKD.getInstance().hadLockPattern()){
				mSecurityFingerprintCb.setChecked(false);
				showTwoBtnDialog(getString(R.string.close_gesture_hint), getString(R.string.confirm), getString(R.string.cancel), mCloseGestureDialog, true);
			}else{
				if(YJKD.getInstance().hasFingerprint()){
					enableFingerPrint(checked);
					CLog.i(TAG, "指纹解锁是否开启11="+YJKD.getInstance().isFingerprintOpen());
				}else{
					enableFingerPrint(false);
					mSecurityFingerprintCb.setChecked(false);
					showOneBtnDialog(getString(R.string.please_add_fingerprint), getString(R.string.got_it));
				}
			}
		}else{
			enableFingerPrint(checked);
			CLog.i(TAG, "指纹解锁是否开启22="+YJKD.getInstance().isFingerprintOpen());
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
		if(checked){
			if(GesturesPresenter.readStatus() && YJKD.getInstance().hadLockPattern()){
				showTwoBtnDialog(getString(R.string.close_gesture_hint), getString(R.string.confirm), getString(R.string.cancel), mCloseGestureDialog, true);
			}else{
				if(YJKD.getInstance().hasFingerprint()){
					enableFingerPrint(checked);
					CLog.i(TAG, "指纹解锁是否开启11="+YJKD.getInstance().isFingerprintOpen());
				}else{
					showOneBtnDialog(getString(R.string.please_add_fingerprint), getString(R.string.got_it));
					mSecurityFingerprintCb.setChecked(false);
				}
			}
			StatService.onEventWithSingleAttribute(this, StatIDSet.EVENT_ID_FINGERPRINT_UNLOCK, StatService.ATTRIBUTE_VALUE_ONE);
		}else{
			enableFingerPrint(checked);
			CLog.i(TAG, "指纹解锁是否开启22="+YJKD.getInstance().isFingerprintOpen());
			StatService.onEventWithSingleAttribute(this, StatIDSet.EVENT_ID_FINGERPRINT_UNLOCK, StatService.ATTRIBUTE_VALUE_TWO);

		}
	}

	private void enableFingerPrint(boolean open){
		YJKD.getInstance().setFingerprintOpen(open);
	}

	private CommonDialog.ButtonClickListener mCloseGestureDialog = new CommonDialog.ButtonClickListener() {
		@Override
		public void onCancel(CommonDialog dialog) {
			dialog.dismiss();
		}

		@Override
		public void onConfirm(CommonDialog dialog) {
			dialog.dismiss();
			enableFingerPrint(true);
			mSecurityFingerprintCb.setChecked(true);
			GesturesPresenter.writeStatus(false);
			YJKD.getInstance().removeChosenPattern();
		}
	};

	private CommonDialog.ButtonClickListener mLogoutDialog = new CommonDialog.ButtonClickListener() {
		@Override
		public void onCancel(CommonDialog dialog) {
			dialog.dismiss();
		}

		@Override
		public void onConfirm(CommonDialog dialog) {
			dialog.dismiss();
			/***
			 * 退出登录成功后停止极光推送服务 不再接收消息
			 */
			JPushInterface.deleteAlias(SecurityActivity.this, 1);
			JPushInterface.stopPush(getApplicationContext());
			mPresenter.logout();
			YJKD.logout();
			MainActivity.start(SecurityActivity.this, true);
			finish();
		}
	};

	@Override
	protected void onDestroy() {
		getSupportLoaderManager().destroyLoader(LOADER_ID);
		super.onDestroy();
	}

	@Override
	public void logoutSuccess() {
	}

	@Override
	public void logoutFail(String msg) {

	}

	@Override
	public void logoutRetCode(String retCode) {

	}
}
